package com.meitianhui.convenitenbuy.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.R;

/**
 * Created by Administrator on 2017/12/1.
 */

public class UpdateDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private DialogClick dialogClick;
    private UpdateDialog dialog = null;

    private TextView mTvSure;
    private TextView mTvCancel;
    private TextView mTvContent;

    public UpdateDialog(Context context) {
        super(context);
    }

    public UpdateDialog(Context context, DialogClick dialogClick) {
        super(context);
        this.context = context;
        this.dialogClick = dialogClick;
        dialog = new UpdateDialog(context);
        dialog.setContentView(R.layout.dialog_update);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        initView(dialog);
        dialog.show();
        //设置dialog的宽度和高度
        try {
            Window win = dialog.getWindow();
            win.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 布局绑定
     */
    private void initView(UpdateDialog dialog) {
        mTvCancel = (TextView) dialog.findViewById(R.id.tv_cancel);
        mTvCancel.setOnClickListener(this);
        mTvSure = (TextView) dialog.findViewById(R.id.tv_sure);
        mTvSure.setOnClickListener(this);
        mTvContent = (TextView) dialog.findViewById(R.id.tv_content);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:    //取消
                if (dialogClick != null) {
                    dialogClick.cancelClick();
                }
                break;
            case R.id.tv_sure:   //确认
                if (dialogClick != null) {
                    dialogClick.submitClick();
                }
                break;
        }
    }

    /**
     * 禁用 “取消” 按钮。
     * 禁用“取消”按钮后，即使设置了{@doce DialogClick.cancelClick()} ，也不能使用。
     */
    public void disableCancelButton() {
        mTvCancel.setVisibility(View.GONE);
    }

    /**
     * 设置更新的内容。
     */
    public void setContent(String content) {
        mTvContent.setText("更新内容：\n" + content);
    }

    public void stopProgressDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }

    public void setOnClickListener(DialogClick l) {
        this.dialogClick = l;
    }

    public interface DialogClick {

        void cancelClick();

        void submitClick();

    }

}